package binTree;

import java.util.ArrayList;
import java.util.List;

//114. 二叉树展开为链表
public class Num114_flatten {
    private List<TreeNode> list = new ArrayList<>();
    public void flatten(TreeNode root) {
        //前序遍历存储节点
        preorderTraversal(root);
        //修改二叉树结构
        for (int i = 1; i < list.size(); i++) {
            TreeNode prev = list.get(i - 1),curr = list.get(i);
            prev.left = null;
            prev.right = curr;
        }


    }
    //二叉树的前序遍历
    public void preorderTraversal(TreeNode root) {
        if (root != null){
            list.add(root);
            preorderTraversal(root.left);
            preorderTraversal(root.right);
        }
    }

}


